const list = [
    {id: 1, pid: 0},
    {id: 2, pid: 0},
    {id: 3, pid: 0},
    {id: 11, pid: 1},
    {id: 12, pid: 1},
    {id: 13, pid: 1},
    {id: 21, pid: 2},
    {id: 22, pid: 2},
    {id: 23, pid: 2},
    {id: 31, pid: 3},
    {id: 32, pid: 3},
    {id: 33, pid: 3},
    {id: 111, pid: 11},
    {id: 221, pid: 22},
    {id: 331, pid: 33},
    {id: 3331, pid: 331}
]

 /*
   * list [] 原数组  
   * id  String  子id
   * pid String  父id
   * */
 
function listToTree(list, id, pid) {
    let temp = {};
    let tree = [];
    list.forEach(item => {
        temp[item[id]] = item
    });
    for(let i in temp) {
        if(temp[i][pid] !== 0) {
            if(!temp[temp[i][pid]].children) {
                temp[temp[i][pid]].children = []
            }
            temp[temp[i][pid]].children.push(temp[i])
        } else {
            tree.push(temp[i])
        }
    }
    return tree;
}

console.log(listToTree(list, 'id', 'pid'));
